package top.soulblack.purchasesale.controller.sale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.Customer;
import top.soulblack.purchasesale.service.CustomerService;

import java.util.Date;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * @Author: 陈俊宇
 * @Description:客户管理Controller
 * @Date: Create in 20:13 2019/8/30
 */

@RequestMapping("/custom_management")
@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @ResponseBody
    @RequestMapping("/updateCustomer")
    public Result<Boolean> updateCustomer(Customer customer){
        if(customerService.updateCustomer(customer)){
            return Result.success(true);
        }
        return Result.error(CUSTOMER_EDIT_ERROR);
    }

    @RequestMapping("/addCustomer")
    @ResponseBody
    public Result<Boolean> addCustomer(Customer customer) {
        // 判断是否存在当前颜色
        if (customerService.existCustomer(customer.getName())) {
            return Result.error(CUSTOMER_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (customer.getSort() > 100 || customer.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        customerService.addCustomer(customer);
        return Result.success(true);
    }

    @RequestMapping("/deleteCustomers")
    @ResponseBody
    public Result<Boolean> deleteCustomer(Integer customersId) {
        // 判断是否存在当前颜色
        if (customerService.deleteCustomer(customersId)) {
            return Result.success(true);
        }
        return Result.error(CUSTOMER_DELETE);
    }


}
